//
//  StatusCell.swift
//  weibo
//
//  Created by 张宏芝 on 15/9/6.
//  Copyright © 2015年 kama. All rights reserved.
//

import UIKit

/// 控件间的距离
let HMStatusCellMargin:CGFloat = 12
/// 头像的大小
let HMStatusIconWidth: CGFloat = 35
 /// 默认的图片的大小
let HMStatusPictureItemWidth:CGFloat = 90
/// 图片之间的间距
let HMStatusPictureItemMargin:CGFloat = 10
 /// 每行的最大图片数量
let HMStatusPictureMaxCount:CGFloat = 3
 /// 配图的最大尺寸
let HMStatusPictureMaxWidth = HMStatusPictureMaxCount * HMStatusPictureItemWidth + (HMStatusPictureMaxCount - 1) * HMStatusPictureItemMargin


protocol StatusCellDelegate: NSObjectProtocol {
    func statusCellDidClickURL(url: NSURL)
}


class StatusCell: UITableViewCell {
    
    weak var cellDelegate: StatusCellDelegate?

/// 微博数据视图模型
    var statusViewModel:StatusViewModel?{
        didSet{
            topView.statusViewModel = statusViewModel
            
//            contentLabel.text = statusViewModel?.status.text
            
//    微博正文
            let statusText = statusViewModel?.status.text ?? ""
            contentLabel.attributedText = EmoticonViewModel.sharedViewModel.emoticonText(statusText, font: contentLabel.font)
            
//            设置配图视图->内部的sizeToFit 计算出大小
            pictureView.statusViewModel = statusViewModel
//            设置配图视图的大小
            pictureViewWidthCons?.constant = pictureView.bounds.width
            pictureViewHeigthCons?.constant = pictureView.bounds.height
            pictureViewTopCons?.constant = statusViewModel?.thumbnailURLs?.count == 0 ? 0 : HMStatusCellMargin
        }
    }
//    宽度约束
    var pictureViewWidthCons: NSLayoutConstraint?
//    高度约束
    var pictureViewHeigthCons: NSLayoutConstraint?
//    顶部约束
    var pictureViewTopCons: NSLayoutConstraint?
    
//    计算指定视图模型对应的行高
    func rowHeight(viewModel:StatusViewModel) ->CGFloat{
//        1.设置视图模型,会调用模型的didSet
        statusViewModel = viewModel
//        2.有了内容之后更新约束
        layoutIfNeeded()
//        3.返回底部视图的最大高度
        return CGRectGetMaxY(bottomView.frame)
    }
    
//    MARK:搭建界面
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
//        表格的cell一定要指定颜色,不要指定clearColor,也不建议指定alpha
        backgroundColor = UIColor.whiteColor()
        
//        顶部分割线
        let SepView = UIView()
        SepView.backgroundColor = UIColor(white: 0.85, alpha: 1.0)
        
        contentView.addSubview(SepView)
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
//        设置cell的布局(框架),默认的宽度与高度是320,44
        let width = UIScreen.mainScreen().bounds.width
        // 1>顶部的分隔视图
       SepView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: 10))
        // 2>顶部视图
        topView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView:SepView, size: CGSize(width: width, height: HMStatusIconWidth + HMStatusCellMargin))
        // 3>正文标签
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: HMStatusCellMargin, y: HMStatusCellMargin))
        // 4>配图
        
        // 5>底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: width, height: 44), offset: CGPoint(x: -HMStatusCellMargin, y: HMStatusCellMargin))
        
        // 指定底部视图相对底边约束
//        bottomView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: contentView, size: nil)
        
        // 指定 label 的代理
        contentLabel.labelDelegate = self

    }
    
//  MARK:懒加载控件
    private lazy var topView:StatusCellTopView = StatusCellTopView()
    lazy var contentLabel: FFLabel = FFLabel(title: nil, color: UIColor.darkGrayColor(), fontSize: 15, layoutWidth: UIScreen.mainScreen().bounds.width - 2 * HMStatusCellMargin)

 /// 配图视图
    var pictureView:StatusPictureView = StatusPictureView()
    lazy var bottomView:StatusCellBottomView = StatusCellBottomView()

}


// MARK: - FFLabelDelegate
extension StatusCell: FFLabelDelegate {
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        print(text)
        
        // 1. 首先判断是否以 http 开头
        if !text.hasPrefix("http://") {
            return
        }
        
        // 2. 生成一个 url
        guard let url = NSURL(string: text) else {
            return
        }
        
        // 3. 通过代理通知控制器
        cellDelegate?.statusCellDidClickURL(url)
    }
}






























